/**
 * 
 */
package com.williamoneil.charts.client.widget;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.canvas.dom.client.Context2d;
import com.williamoneil.charts.client.data.ChartBoundsRect;
import com.williamoneil.charts.client.data.ChartElementsData;
import com.williamoneil.charts.client.data.DateAxis;
import com.williamoneil.charts.client.data.LineData;
import com.williamoneil.charts.client.data.PriceAxis;
import com.williamoneil.charts.client.data.RelativeStrengthLine;
import com.williamoneil.charts.client.data.Tick;
import com.williamoneil.charts.client.util.ColorEntityType;
import com.williamoneil.charts.client.util.Colors;
import com.williamoneil.charts.shared.TradeDateType;

/**
 * @author gudipati
 *
 */
public class RelativeStrengthLineLayer extends ChartOverlay {
	
	public RelativeStrengthLineLayer(final ChartBoundsRect chartBounds) {
		super(chartBounds);
	}
	
	/* (non-Javadoc)
	 * @see com.williamoneil.charts.client.widget.ChartOverlay#drawOverlay()
	 */
	@Override
	public void drawOverlay() {
		final ChartElementsData elements = super.getChartElements();
		if(elements == null) {
			return;
		}
		
		drawRsLine();
	}
	
	private void drawRsLine() {
		final ChartElementsData elements = super.getChartElements();
		final DateAxis dateAxis = elements.getDateAxis();
		final ChartBoundsRect bounds = super.getChartBoundsRect();
		final PriceAxis priceAxis = elements.getPriceAxis();
		final Context2d context = super.getGraphicsContext();

		Integer rsOffset = null;
		final List<LineData> lineList = new ArrayList<LineData>();
		for(final Tick aData : elements.getTicks()) {
			if(aData.getTradeDateType() != TradeDateType.TRADE_DATE) {
				continue;
			}
			
			final RelativeStrengthLine rsLine = aData.getRsLine();
			if(rsLine == null) {
				continue;
			}
			
			final int x = dateAxis.getX(aData.getDate());
			if(!bounds.isXWithinBounds(x)) {
				break;
			}
			
			int y = priceAxis.getY(rsLine.getPrice(), bounds);
			
			// set default multiple if none is already set
			// default multiple is setup in such a way that the last-value of the RS line is drawn below price 
			if(rsOffset == null) {
				rsOffset = 0;
				/*
				final int lastTickLowY = priceAxis.getY(aData.getPrice().getLow().doubleValue(), bounds);
				
				final int maxRsOffset = Math.min(bounds.getHeightWithoutScale()- lastTickLowY - 20, 100);
				rsOffset = lastTickLowY + maxRsOffset - y;
				*/
			}
			
			if(!bounds.isYWithinBounds(y)) {
				continue;
			}
			
			final LineData line = new LineData(x, y+rsOffset);
			lineList.add(line);
		}
		
		if(lineList.size() > 0) {
			context.beginPath();
			String color = Colors.getColorTheme().getColor(ColorEntityType.RS_LINE);
			context.setStrokeStyle(color);
			context.setFillStyle(color);

			context.moveTo(lineList.get(0).getX(), lineList.get(0).getY());
			for(int i=0;i<lineList.size();i++) {
				final LineData aData = lineList.get(i);
				context.lineTo(aData.getX(), aData.getY());
			}
			context.stroke();
		}
	}
}